package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlhnlzpzjlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlhnlzpzjlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjlhnlzpzjlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjlhnlzpzjlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjlhnlzpzjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料含泥量、泥块针片状颗粒记录详情Service业务层处理
 *
 * @author tll
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlhnlzpzjlbBaDetailServiceImpl implements IReportJlCjlhnlzpzjlbBaDetailService {

    private final ReportJlCjlhnlzpzjlbBaDetailMapper baseMapper;

    /**
     * 查询粗集料含泥量、泥块针片状颗粒记录详情
     */
    @Override
    public ReportJlCjlhnlzpzjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询粗集料含泥量、泥块针片状颗粒记录详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjlhnlzpzjlbBaDetailVo> queryPageList(ReportJlCjlhnlzpzjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlhnlzpzjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlhnlzpzjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料含泥量、泥块针片状颗粒记录详情列表
     */
    @Override
    public List<ReportJlCjlhnlzpzjlbBaDetailVo> queryList(ReportJlCjlhnlzpzjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjlhnlzpzjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlhnlzpzjlbBaDetail> buildQueryWrapper(ReportJlCjlhnlzpzjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlhnlzpzjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjlhnlzpzjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getGuizyfsyzzl() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfsyzzl, bo.getGuizyfsyzzl());
        lqw.eq(bo.getGuizyfljfks() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfljfks, bo.getGuizyfljfks());
        lqw.eq(bo.getGuizyfzzklzl() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfzzklzl, bo.getGuizyfzzklzl());
        lqw.eq(bo.getGuizyfpzklzl() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfpzklzl, bo.getGuizyfpzklzl());
        lqw.eq(bo.getGuizyfzpzklzzl() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfzpzklzzl, bo.getGuizyfzpzklzzl());
        lqw.eq(bo.getGuizyfzpzklhlcz() != null, ReportJlCjlhnlzpzjlbBaDetail::getGuizyfzpzklhlcz, bo.getGuizyfzpzklhlcz());
        return lqw;
    }

    /**
     * 新增粗集料含泥量、泥块针片状颗粒记录详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjlhnlzpzjlbBaDetailBo bo) {
        ReportJlCjlhnlzpzjlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjlhnlzpzjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改粗集料含泥量、泥块针片状颗粒记录详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjlhnlzpzjlbBaDetailBo bo) {
        ReportJlCjlhnlzpzjlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjlhnlzpzjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlhnlzpzjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料含泥量、泥块针片状颗粒记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
